package com.qianfeng.es.service.impl;

//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                  佛祖镇楼                  BUG辟易
//          佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//


import com.qianfeng.es.service.DocService;
import com.qianfeng.es.utils.ConnectionUtils;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.util.Map;

/**
 * Created by Jackiechan on 2022/10/12 11:40
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
public class DocServiceImpl implements DocService {


    @Override
    public void addDoc(String index, Map source) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        IndexResponse response = client.index(new IndexRequest(index).source(source), RequestOptions.DEFAULT);
        System.err.println(response.toString());

    }

    @Override
    public void addDoc(String index, String jsonSource) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        IndexResponse response = client.index(new IndexRequest(index).source(jsonSource, XContentType.JSON), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void addDocWithId(String index, String jsonSource, String id) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        IndexResponse response = client.index(new IndexRequest(index).source(jsonSource, XContentType.JSON).id(id), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void getDocById(String index, String id) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        GetResponse response = client.get(new GetRequest(index, id), RequestOptions.DEFAULT);
        System.err.println(response.getSourceAsString());
    }

    @Override
    public void updateDocById(String index, String id, String jsonSource) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        UpdateResponse response = client.update(new UpdateRequest(index, id).doc(jsonSource, XContentType.JSON), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }

    @Override
    public void deleteDocById(String index, String id) throws Exception {
        RestHighLevelClient client = ConnectionUtils.getClient();
        DeleteResponse response = client.delete(new DeleteRequest(index, id), RequestOptions.DEFAULT);
        System.err.println(response.toString());
    }
}
